package m.jclecai.com.dialog;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;

/**
 * Created by Dinosa on 2017/11/15.
 */

public class CustomFollowOrderDialog extends BaseDialog {

    @BindView(R.id.rv_recycler_view)
    RecyclerView mRvRecyclerView;

    @BindView(R.id.tv_cancel)
    TextView mTvCancel;

    @BindView(R.id.tv_sure)
    TextView mTvSure;
    private List<LotteryBean> mData;

    public CustomFollowOrderDialog(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_custom_follow_order);
        //设置布局；
        getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initLottery();
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        mRvRecyclerView.setAdapter(new CustomFollowLotteryAdapter(mData));
        mRvRecyclerView.setLayoutManager(layoutManager);
    }

    private void initLottery() {

        mData = new ArrayList<>();
       // mData.add(new LotteryBean("双色球","ssq"));
      //  mData.add(new LotteryBean("大乐透","dlt"));
      //  mData.add(new LotteryBean("福彩3D","sd"));
      //  mData.add(new LotteryBean("排列三","pls"));
        mData.add(new LotteryBean("竞彩足球","jczq"));
        mData.add(new LotteryBean("单场","bjdc"));
      //  mData.add(new LotteryBean("胜负彩","sfc"));
        mData.add(new LotteryBean("竞彩篮球","jclq"));
    }

    @OnClick(R.id.tv_cancel)
    public void onClickCancel(View view){
        dismiss();
    }

    @OnClick(R.id.tv_sure)
    public void onClickSure(View view){
        if (mListener != null) {
            mListener.onSureClick(getCheckedLottery());
        }
        dismiss();
    }


    public interface OnSureClickListener{
        void onSureClick(List<LotteryBean> mData);
    }

    public OnSureClickListener mListener;

    //这里我们要做的一个操作就是将用户点击的条目暴露出去
    public OnSureClickListener getListener() {
        return mListener;
    }

    public void setListener(OnSureClickListener listener) {
        mListener = listener;
    }

    public class CustomFollowLotteryAdapter extends BaseRecyclerViewAdapter<LotteryBean>{

        public CustomFollowLotteryAdapter(List<LotteryBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_custom_follow_order_lottery;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final LotteryBean bean, final int position) {
            final CheckBox checkBox = (CheckBox) holder.getView(R.id.cb_lottery_name);

            checkBox.setChecked(bean.isChecked);
            checkBox.setText(bean.lotteryName);

            checkBox.setTag(bean);

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Object tag = ((CheckBox) buttonView).getTag();
                    ((LotteryBean) tag).isChecked=isChecked;
                }
            });
        }
    }

    public class LotteryBean{

        public boolean isChecked;
        public String lotteryName;
        public String type;

        public LotteryBean(String lotteryName,String type) {
            this.lotteryName = lotteryName;
            setChecked(false);
            this.type=type;
        }

        public boolean isChecked() {
            return isChecked;
        }

        public void setChecked(boolean checked) {
            isChecked = checked;
        }

        public String getLotteryName() {
            return lotteryName;
        }

        public void setLotteryName(String lotteryName) {
            this.lotteryName = lotteryName;
        }
    }

    public List<LotteryBean> getCheckedLottery(){
        List<LotteryBean> datas= new ArrayList<>();

        for (LotteryBean lotteryBean : mData) {
            if (lotteryBean.isChecked) {
                datas.add(lotteryBean);
            }
        }
        return datas;
    }
}
